package org.tloss.web.editor.enity.editors;

import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;
import org.tloss.web.editor.database.editors.jdbc.Column;

public class ColumnProperties implements IPropertySource {
	Column column;
	public final static String TYPENAME = "TYPENAME";
	public final static String COLUMNSIZE = "COLUMNSIZE";
	public final static String NULLABLE = "NULLABLE";
	public final static String DECIMALDIGITS = "DECIMALDIGITS";
	public final static String REMARKS = "REMARKS";
	public final static String ISAUTOINCREMENT = "ISAUTOINCREMENT";
	public final static String MAPPING_NAME = "MAPPING_NAME";
	public final static String MAPPING_TYPE = "MAPPING_TYPE";
	public final static String MAPPING_ISCOLLECTION = "MAPPING_ISCOLLECTION";
	public final static String MAPPING_COLECTIONTYPE = "MAPPING_COLECTIONTYPE";
	public final static String[] COLLECTION_TYPES = { "None", "Array",
			"Collection" };

	private PropertyDescriptor type = new PropertyDescriptor(TYPENAME, "Type");
	private PropertyDescriptor size = new PropertyDescriptor(COLUMNSIZE, "Size");
	private PropertyDescriptor nullable = new PropertyDescriptor(NULLABLE,
			"Nullable");
	private PropertyDescriptor decimalDigits = new PropertyDescriptor(
			DECIMALDIGITS, "Decimal Digits");
	private PropertyDescriptor remarks = new PropertyDescriptor(REMARKS,
			"Remarks");
	private PropertyDescriptor isAutoincrement = new PropertyDescriptor(
			ISAUTOINCREMENT, "Auto Increment");
	private PropertyDescriptor mappingName = new TextPropertyDescriptor(
			MAPPING_NAME, "Mapping name");
	private JavaClassPropertyDescriptor mappingType = new JavaClassPropertyDescriptor(
			MAPPING_TYPE, "Mapping type");
	private ComboBoxPropertyDescriptor mappingIscollection = new ComboBoxPropertyDescriptor(
			MAPPING_ISCOLLECTION, "Collection", COLLECTION_TYPES);
	private JavaClassPropertyDescriptor mappingColectionType = new JavaClassPropertyDescriptor(
			MAPPING_COLECTIONTYPE, "Colection Type");
	IPropertyDescriptor[] descriptors = new IPropertyDescriptor[] { type, size,
			nullable, decimalDigits, remarks, isAutoincrement, mappingName,
			mappingType, mappingIscollection, mappingColectionType };

	protected void firePropertyChanged(String propName, Object value) {
		if (listener != null) {
			listener.resourceChanged(null);
		}
	}

	IResourceChangeListener listener;

	/**
	 * Creates a new ButtonElementProperties.
	 * 
	 * @param element
	 *            the element whose properties this instance represents
	 */
	public ColumnProperties(Column column, IResourceChangeListener listener) {
		this.column = column;
		this.listener = listener;

		mappingName.setCategory("Mapping");
		mappingType.setCategory("Mapping");
		mappingIscollection.setCategory("Mapping");
		mappingColectionType.setCategory("Mapping");

		type.setCategory("Other Information");
		size.setCategory("Other Information");
		nullable.setCategory("Other Information");
		decimalDigits.setCategory("Other Information");
		remarks.setCategory("Other Information");
		isAutoincrement.setCategory("Other Information");

	}

	/**
	 * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
	 */
	public Object getEditableValue() {
		return this;
	}

	/**
	 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
	 */
	public IPropertyDescriptor[] getPropertyDescriptors() {
		return descriptors;

	}

	/**
	 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(Object)
	 */
	public Object getPropertyValue(Object name) {
		if (TYPENAME.equals(name)) {
			return column.getTypeName();
		} else if (COLUMNSIZE.equals(name)) {
			return column.getColumnSize();
		} else if (NULLABLE.equals(name)) {
			return column.getNullable();
		} else if (DECIMALDIGITS.equals(name)) {
			return column.getDecimalDigits();
		} else if (REMARKS.equals(name)) {
			return column.getRemarks();
		} else if (ISAUTOINCREMENT.equals(name)) {
			return column.getIsAutoincrement();
		} else if (MAPPING_NAME.equals(name)) {
			return column.getMapping().getName();
		} else if (MAPPING_TYPE.equals(name)) {
			return column.getMapping().getType();
		} else if (MAPPING_ISCOLLECTION.equals(name)) {
			if (column.getMapping().getIsCollection()) {
				return 2;
			} else if (column.getMapping().getIsArray()) {
				return 1;
			} else {
				return 0;
			}
		} else if (MAPPING_COLECTIONTYPE.equals(name)) {
			return column.getMapping().getColectionType();
		}
		return null;
	}

	/**
	 * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(Object)
	 */
	public boolean isPropertySet(Object id) {
		return false;
	}

	/**
	 * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(Object)
	 */
	public void resetPropertyValue(Object id) {
	}

	/**
	 * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(Object,
	 *      Object)
	 */
	public void setPropertyValue(Object name, Object value) {
		if (MAPPING_NAME.equals(name)) {
			column.getMapping().setName((String) value);
		} else if (MAPPING_TYPE.equals(name)) {
			column.getMapping().setType((String) value);
		} else if (MAPPING_ISCOLLECTION.equals(name)) {
			if (Integer.valueOf(2).equals(value)) {
				column.getMapping().setIsCollection(true);
				column.getMapping().setIsArray(false);
			} else if (Integer.valueOf(1).equals(value)) {
				column.getMapping().setIsCollection(false);
				column.getMapping().setIsArray(true);
			} else {
				column.getMapping().setIsCollection(false);
				column.getMapping().setIsArray(false);
			}
		} else if (MAPPING_COLECTIONTYPE.equals(name)) {
			column.getMapping().setColectionType((String) value);
		}
		firePropertyChanged((String) name, value);
	}

}
